package cn.wildfire.chat.app.extendsion.classes;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.sourcemuch.im.caiba.wxapi.WXUserInfo;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cn.wildfire.chat.app.Config;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.WfcBaseActivity;

/**
 * Create by HEKE, 2019/10/17 10:53
 * TODO 绑定微信接口
 */
public class BindWeiXinRequest {

    private WfcBaseActivity mActivity;
    private WXUserInfo wxUserInfo;

    private IWXAPI iwxapi;
    private BroadcastReceiver wxBroadcastReceiver;

    private OnBinWeiXinListener onBinWeiXinListener;

    public BindWeiXinRequest() {
        super();
    }

    public BindWeiXinRequest(WfcBaseActivity mActivity, OnBinWeiXinListener onBinWeiXinListener) {
        try {
            this.onBinWeiXinListener = onBinWeiXinListener;
            this.iwxapi = WXAPIFactory.createWXAPI(mActivity, Config.WXAppID, true);
            this.iwxapi.registerApp(Config.WXAppID);
            //建议动态监听微信启动广播进行注册到微信
            wxBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // 将该app注册到微信
                    iwxapi.registerApp(Config.WXAppID);
                }
            };
            mActivity.registerReceiver(wxBroadcastReceiver, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnBinWeiXinListener(OnBinWeiXinListener onBinWeiXinListener) {
        this.onBinWeiXinListener = onBinWeiXinListener;
    }

    /**
     * 发送请求
     *
     * @param context
     * @param wxUserInfo
     */
    public void req(Context context, WXUserInfo wxUserInfo) {
        if (wxUserInfo == null)
            return;
        this.wxUserInfo = wxUserInfo;
        try {
            OkHttpRequest.getNewInstance(context).requestAsynJsonObject3(ServerUrlClient.BIND_TENCENT_WEIXIN, this.wxUserInfo, new ReqCallBack<String>() {
                @Override
                public void onReqSuccess(String result) {
                    try {
                        if (!TextUtils.isTextEmpty(result)) {
                            ResponseEntity responseEntity = new ResponseEntity(result);
                            if (responseEntity.isSuccess()) {
                                if (onBinWeiXinListener != null) {
                                    onBinWeiXinListener.bindWX(true, responseEntity.message);
                                }
                            } else {
                                if (onBinWeiXinListener != null) {
                                    onBinWeiXinListener.bindWX(false, responseEntity.message);
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onReqFailed(String errorMsg) {
                    Log.e("### BindWeiXinRequest ", " errorMsg : " + errorMsg);
                    if (onBinWeiXinListener != null) {
                        onBinWeiXinListener.bindWX(false, errorMsg);
                    }
                }
            }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拉起微信授权
     */
    public void pullWeiXinAuth() {
        if (iwxapi == null)
            return;
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "sourcemuch_wechat_sdk_bindweixin";
        iwxapi.sendReq(req);
    }

    /**
     * 释放广播
     */
    public void unregister() {
        try {
            if (wxBroadcastReceiver != null) {
                mActivity.unregisterReceiver(wxBroadcastReceiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create by HEKE, 2019/10/17 10:59
     * TODO 回调绑定微信接口
     */
    public interface OnBinWeiXinListener {
        void bindWX(boolean success, String message);
    }

}
